﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace qdata
{
    public partial class doData : Form
    {
        public doData()
        {
            InitializeComponent();
          
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            string DongcheShowdata = "";
            string huocheShowdata = "";
            int HSeqno = 0;
            int DHSeqno = 0;
            string distanct = "";
            GetCarStation gc = new GetCarStation();
            DataTable dt = gc.ExecuteDataTable(string.Format("select * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", txtscity.Text.Trim(), txtecity.Text.Trim()));
            string huocetemp= ReaFile("huocetemp.txt") ;
            if (dt!=null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["checi"].ToString().ToUpper().StartsWith("D") || dr["checi"].ToString().ToUpper().StartsWith("G"))
                    {
                        DHSeqno++; 
                        dr["seqno"] = DHSeqno;
                        DongcheShowdata += ChangeTempData(dr, huocetemp);
                    }
                    else
                    {
                        HSeqno++;
                        dr["seqno"] = HSeqno;
                        huocheShowdata += ChangeTempData(dr, huocetemp);
                    }
                    if (distanct==string.Empty  && dr["lucheng"].ToString() != string.Empty)
                    {
                        distanct = dr["lucheng"].ToString();
                    }
                    if ((DHSeqno + HSeqno) > int.Parse(ConfigurationSettings.AppSettings["HuocheCount"].ToString()))
                        break;
                }


                string huoceOthertemp = ReaFile("huocetempother.txt");
                txthuoche.Text = huocheShowdata;
                txtDongche.Text = DongcheShowdata;
               
               OtherInfo(huoceOthertemp, dt.Rows.Count.ToString(), DHSeqno.ToString(), HSeqno.ToString(), distanct);

               DataTable dtinfo = gc.ExecuteDataTable(string.Format("select * from [TotalInfo] where Scity ='{0}' and  Ecity = '{1}'", txtscity.Text.Trim(), txtecity.Text.Trim()));

               string jttemp = ReaFile("jt.txt");
               if (dtinfo != null && dtinfo.Rows.Count > 0)
               {
                   jttemp = ChangeTempData(dtinfo.Rows[0], jttemp);
                   jttemp = jttemp.Replace("|CHECILIST|", huocheShowdata + DongcheShowdata);
                   txtOther.Text = jttemp;
               }
               txtqiche.Text =  GetBus();
            }
        }

       

        public void  OtherInfo(string huoceOthertemp,string AllTotal,string DTtotal,string HCtotal,string distanct)
        {
            GetCarStation gc = new GetCarStation();
            DataTable dt1 = gc.ExecuteDataTable(string.Format("select top 1 * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", txtscity.Text.Trim(), txtecity.Text.Trim()) + " order by sdate asc  ");
            DataTable dt2 = gc.ExecuteDataTable(string.Format("select top 1 * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", txtscity.Text.Trim(), txtecity.Text.Trim()) + " order by sdate desc   ");
            if (dt1 == null || dt1.Rows.Count <= 0) return;
            string ZCheci= dt1.Rows[0]["checi"].ToString();
            string ZDate=dt1.Rows[0]["sdate"].ToString();
            string LCheci=dt2.Rows[0]["checi"].ToString();
            string LDate=dt2.Rows[0]["sdate"].ToString();
            //huoceOthertemp = huoceOthertemp.Replace("|SCITY|", txtscity.Text.Trim());
            //huoceOthertemp = huoceOthertemp.Replace("|ECITY|", txtecity.Text.Trim());
            //huoceOthertemp = huoceOthertemp.Replace("|HUOCTOTAL|", AllTotal);
            //huoceOthertemp = huoceOthertemp.Replace("|DTTOTAL|", DTtotal);
            //huoceOthertemp = huoceOthertemp.Replace("|HCTOTAL|", HCtotal);
            //huoceOthertemp = huoceOthertemp.Replace("|FCHECI|", ZCheci);
            //huoceOthertemp = huoceOthertemp.Replace("|FDATE|", ZDate);
            //huoceOthertemp = huoceOthertemp.Replace("|LCHECI|", LCheci);
            //huoceOthertemp = huoceOthertemp.Replace("|LDATE|", LDate);


            DataTable dt3 = gc.ExecuteDataTable(string.Format("select top 1 * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", txtscity.Text.Trim(), txtecity.Text.Trim()) + " order by piaojia asc  ");
            DataTable dt4 = gc.ExecuteDataTable(string.Format("select top 1 * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", txtscity.Text.Trim(), txtecity.Text.Trim()) + " order by piaojia desc   ");

            string Cheapest = dt3.Rows[0]["piaojia"].ToString();
            string chpcheci = dt3.Rows[0]["checi"].ToString();
            string expensive = dt4.Rows[0]["piaojia"].ToString();
            string expensivecheci = dt4.Rows[0]["checi"].ToString();

            DataTable dt = gc.ExecuteDataTable(string.Format("select * from [TotalInfo] where Scity ='{0}' and  Ecity = '{1}'", txtscity.Text.Trim(), txtecity.Text.Trim()));
            if (dt != null && dt.Rows.Count > 0)
            {
                string ustr = "update [TotalInfo] set Distance='{0}',  ZCheci='{1}',ZDate='{2}',LCheci='{3}',LDate='{4}' , Total='{5}',  DTotal='{6}' , HTotal='{7}',Cheapest='{10}',chpcheci='{11}',expensive='{12}',expensivecheci='{13}',EndSite='{14}'  where Scity ='{8}' and  Ecity = '{9}'";

                ustr = string.Format(ustr, distanct, ZCheci, ZDate, LCheci, LDate, AllTotal, DTtotal, HCtotal, txtscity.Text.Trim(), txtecity.Text.Trim(), Cheapest, chpcheci, expensive, expensivecheci, txtsite.Text);
                gc.ExecuteDataTable(ustr);
            }
            else
            {
                string ustr = "insert into [TotalInfo] (Distance ,ZCheci ,ZDate,LCheci,LDate,Total,DTotal,HTotal,Scity,Ecity ,Cheapest,chpcheci,expensive,expensivecheci,EndSite)  values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}')";

                ustr = string.Format(ustr, distanct, ZCheci, ZDate, LCheci, LDate, AllTotal, DTtotal, HCtotal, txtscity.Text.Trim(), txtecity.Text.Trim(), Cheapest, chpcheci, expensive, expensivecheci, txtsite.Text );
                gc.ExecuteDataTable(ustr);
            }

             
        }

        public string ChangeTempData(DataRow dr ,string datatemp)
        { 
            DataTable dt =dr.Table ;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                datatemp = datatemp.Replace("|" + dt.Columns[i].ColumnName.ToUpper() + "|", dr[dt.Columns[i].ColumnName].ToString());
            
            }
            return datatemp;
        }

        public string GetBus()
        {

            string bustemp = ReaFile("bustemp.txt");
            string temp = "";
            GetCarStation gc = new GetCarStation();
            DataTable dt = gc.ExecuteDataTable(string.Format("select a.*, b.Address,b.Descrtion,b.Phone from Carbanci as a left join CarStation as b on a.StationName=b.Name where a.Scity like '%{0}%' and  a.ECity like '%{1}%'", txtscity.Text.Trim(), txtecity.Text.Trim())  );
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count ; i++)
                {
                    temp += ChangeTempData(dt.Rows[i], bustemp);
                }
            }
            return temp;
        }

        public string  ReaFile(string filename )
        {
            string str5=Application.StartupPath;
            StreamReader sr = File.OpenText(@str5 + "\\" + filename);
            string nextLine;
            string content = string.Empty;
            while ((nextLine = sr.ReadLine()) != null)
            {
                content += nextLine+"\n";
            }
            sr.Close();
            return content;
        }

        
    }
}
